package com.freecat.utils;


import com.baidu.mapapi.model.LatLng;

import java.util.HashMap;


public class MapFixUtil {

    final static double pi = 3.14159265358979324;
    final static double a = 6378245.0;
    final static double ee = 0.00669342162296594323;
    final static double x_pi=3.14159265358979324 * 3000.0 / 180.0;

    public static double[] transform(double wgLat, double wgLon) {
        double[] latlng = new double[2];
        if (outOfChina(wgLat, wgLon)) {
            latlng[0] = wgLat;
            latlng[1] = wgLon;
            return latlng;
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        latlng[0] = wgLat + dLat;
        latlng[1] = wgLon + dLon;
        return latlng;
    }

    private static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        return lat < 0.8293 || lat > 55.8271;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    public static HashMap BaiduToMap(double lon1, double lat1){
        double mars_point_lon;
        double mars_point_lat;

        HashMap<String, Double> mars_point=new HashMap<String, Double>();

        double x=lon1-0.0065;
        double y=lat1-0.006;
        double z= Math.sqrt(x*x+y*y)- 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        mars_point_lon=z * Math.cos(theta);
        mars_point_lat=z * Math.sin(theta);

        mars_point.put("mars_point_lon",mars_point_lon);
        mars_point.put("mars_point_lat",mars_point_lat);

        return mars_point;
    }

    //火星坐标转百度
    public static HashMap marsTobaidu(double lon1, double lat1){
        HashMap<String, Double> baidu_point=new HashMap<String, Double>();
        double x=lon1;

        double y=lat1;

        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);

        double  baidu_point_lon = z * Math.cos(theta) + 0.0065;
        double  baidu_point_lat = z * Math.sin(theta) + 0.006;

        baidu_point.put("baidu_point_lon",baidu_point_lon);
        baidu_point.put("baidu_point_lat",baidu_point_lat);

        return baidu_point;
    }




    private LatLng bd_decrypt(double lon, double lat)
    {
        double X_PI = Math.PI * 3000.0 / 180.0;
        double x = lon - 0.0065 ;
        double y = lat - 0.006;
        double z = Math.sqrt(x*x+y*y) + 0.00002 * Math.sin(y*X_PI) ;
        double temp = Math.atan2(y, x)  + 0.000003 * Math.cos(x*X_PI);

        double gdLon = z * Math.cos(temp) + 0.0065;
        double gdLat = z * Math.sin(temp) + 0.006;
        LatLng newcenpt = new LatLng(gdLat, gdLon);
        return newcenpt;
    }

    //百度坐标转高德坐标展示
    public static LatLng bd09_To_Gcj02(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new LatLng(gg_lat, gg_lon);
    }

//    private LatLng pianyi(double lon,double lat)
//    {
//        double x = lon; double y = lat;
//        double z = Math.sqrt(x*x+y*y) + 0.00002 *Math.sin(y*Math.PI) ;
//        double temp =Math.atan2(y, x)  + 0.000003 * Math.cos(x*Math.PI);
//
//        double bdLon = z * Math.cos(temp) + 0.0065;
//        double bdLat = z * Math.sin(temp) + 0.006;
//        LatLng newcenpt = new LatLng(bdLat, bdLon);
//        return newcenpt;
//    }
//


}
